﻿var mailStatus = true;
var vmailStatus = true;
var pwdStatus = true;
var vpwdStatus = true;
var verifyCodeStatus = true;
var agreementStatus = true;
var checkOK = true;


$(document).ready(function() {
	renewVerifyImage();
	$("#email").focus();
   //	$("#agreementLink").click(function() {
	//	window.open("", '用户服务协议', 'height=155, width=580,toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, status=no');
	//	return false;
	//});


	$("input").focus(function() {
		$(this).addClass("focus");
	})
    .blur(function() {
    	$(this).removeClass("focus");
    });

	$("#email").focus(function() {
		$(this).next("span").addClass("note").removeClass("ok").removeClass("error").text("请填写有效的Email地址并确认该地址未被注册。接收订单通知、优惠信息等");
	});

	$("#r_email").focus(function() {
		$(this).next("span").addClass("note").removeClass("ok").removeClass("error").text("两次输入的Email地址要一致");
	});

	$("#pwd").focus(function() {
		$(this).next("span").addClass("note").removeClass("ok").removeClass("error").text("密码必须是6-30位字符，请正确输入");
	});

	$("#rpwd").focus(function() {
		$(this).next("span").addClass("note").removeClass("ok").removeClass("error").text("两次输入的密码要一致");
	});

	$("#checkimg").focus(function() {
		$(this).siblings("span").addClass("note").removeClass("ok").removeClass("error").text("请输入四位验证码");
		//$("#spanServerError").hide();
	});

	$("#email").blur(function() { CheckEmail($(this)); });

	$("#r_email").blur(function() { CheckVEmail($(this)); });

	$("#pwd").blur(function() { CheckPwd($(this)); });

	$("#rpwd").blur(function() { CheckVPwd($(this)) });

	$("#checkimg").blur(function() { CheckVeryCode($(this)); });
});


function CheckEmail(obj) {
	var regs = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
	if ($(obj).val() != "") {
		var email = $(obj).val();
		if (!regs.test(email)) {
			$(obj).next("span").addClass("note error").removeClass("ok").text("请输入正确的Email地址");
			mailStatus = false;
		}
		else {
			
			//			$.post($("#appPath").val() + "UserUtils.axd", { mail: $(obj).val(), type: "mail" }, function(data) {
			//				if (data == "1") {
			//					$(obj).next("span").addClass("ok").text("");
			//					mailStatus = true;
			//				}
			//				else {
			//					$(obj).next("span").html(data).addClass("note error").removeClass("ok");
			//					mailStatus = false;
			//				}
		    //			})

		    $.ContentLoader("/member/CheckuserEmail.aspx?userName=" + $(obj).val(), "",
					function(data) {
					    //alert(data);
					    //var msg = eval('(' + data + ')');

					    if (data == "no") {

					        regFlag = false;
					        validateOnce.Exists = true;
					        $("#tip_txtRegisterEmailError").addClass("onError").css("display", "inline").html("该Email已经被注册，请使用未被注册的Email");
					        mailStatus = false;
					    }
					    else {
					        validateOnce.Exists = false;
					        $(obj).next("span").addClass("ok").text("");
					        mailStatus = true;
					    }
					    //if (msg.d == "false") {
					    //	regFlag = false;
					    //validateOnce.Exists = true;
					    //$("#tip_txtRegisterEmailError").addClass("onError").css("display", "inline").html("该Email已经被注册，请使用未被注册的Email地址，如有疑问，请<a href=\"http://www.m18.com/article/help.aspx?documentid=210\">咨询客服</a>。");
					    //	$(obj).next("span").html("该Email已被注册！请使用其他Email地址进行注册！<br/><em class=\"mr10 loud\">使用该地址<a href=\"http://login.m18.com/Default.aspx?Uid=" + $(obj).val() + "\"class=\"a2\">登录</a></em><em class=\"loud\">忘记密码请点击<a href=\"http://www.m18.com/Contact/ContactGetPwdByMail.aspx\"class=\"a2\">找回密码</a></em>").addClass("note error").removeClass("ok");
					    //	mailStatus = false;
					    //}
					    //else {
					    //validateOnce.Exists = false;
					    //	$(obj).next("span").addClass("ok").text("");
					    //	mailStatus = true;
					    //}
					},
					function() {
					    //alert("服务器错误！");
					});
		}
	}
	else {
		$(obj).next("span").addClass("note error").removeClass("ok").text("请输入正确的Email地址");
		mailStatus = false;
	}
}


function CheckVEmail(obj) {
	if ($(obj).val() != "") {
		if ($(obj).val() != $("#email").val()) {
			$(obj).next("span").text("两次输入的Email地址不一致").addClass("error").removeClass("ok");
			vmailStatus = false;
		}
		else {
			$(obj).next("span").addClass("ok").text("");
			vmailStatus = true;
		}
	}
	else {
		$(obj).next("span").addClass("note error").text("请再输一次你的Email地址").removeClass("ok");
		vmailStatus = false;
	}
}


function CheckPwd(obj) {
	if ($(obj).val() != "") {
		var reg = /^(\w|@|#|\$){6,30}/;
		if (!reg.test($("#pwd").val())) {
			$(obj).next("span").addClass("note error").removeClass("ok").text("密码必须是6-30位字符，请正确输入");
			pwdStatus = false;
		}
		else {
			$(obj).next("span").addClass("ok").text("");
			pwdStatus = true;
		}
	}
	else {
		$(obj).next("span").addClass("note error").removeClass("ok").text("密码必须是6-30位字符，请正确输入");
		pwdStatus = false;
	}
}



function CheckVPwd(obj) {
	if ($(obj).val() != "") {
		if ($(obj).val() != $("#pwd").val()) {
			$(obj).next("span").text("您两次输入的密码不一致，请重新输入").addClass("note error").removeClass("ok");
			vpwdStatus = false;
		}
		else {
			$(obj).next("span").addClass("note ok").text("");
			vpwdStatus = true;
		}
	} else {
		$(obj).next("span").addClass("note error").removeClass("ok").text("请再输一次你的密码");
		vpwdStatus = false;
	}
}


function CheckVeryCode(obj) {
	if ($(obj).val().length == 4 && $(obj).val()==$("#imgc").val()) {
		//$("#spanVerifyCodeTip").addClass("note ok").text("");
		//$(obj).val==

		$("#spanVerifyCodeTip").removeClass().text("");
		//$("#ctl00_cpBody_checkimg").text("");
		verifyCodeStatus = true;
	}
	else {
		$("#spanVerifyCodeTip").addClass("note error").text("请输入正确的四位验证码");
		verifyCodeStatus = false;
	}
}


function CheckAgreement(obj) {
	if ($(obj).attr("checked") == true) {
		$(obj).siblings("span").removeClass("note error").text("")
		agreementStatus = true;
	}
	else {
		$(obj).siblings("span").addClass("note error").text("请先阅读并同意《用户服务协议》");
		agreementStatus = false;
	}
}


function renewVerifyImage() {
	//var img = $("#appPath").val() + "VerifyImage.axd?seed=" + Math.round(Math.random() * 100000);
	//$(".checkimg").attr("src", img);
	//var vguid = $("#hidVerifyCodeReg").val();
	//---GetVerifyCode("register", $("#hidVerifyCodeReg").val());
}
function CheckRegister() {
	CheckEmail($("#email"));
	CheckVEmail($("#r_email"));
	CheckPwd($("#pwd"));
	CheckVPwd($("#rpwd"));
	CheckVeryCode($("#checkimg"));
	//CheckAgreement($("#chkAgreement"));
	if (!mailStatus || !vmailStatus || !pwdStatus || !vpwdStatus || !verifyCodeStatus ) {
		checkOK = false;
	}
	else {
		checkOK = true;
	}
	return checkOK;
}
//取验证码
function GetVerifyCode(catalog, oldGuid) {
	var VerifyCodeUrl = $("#hidVerifyCodeUrl").val();
	$.ContentLoader(loginService + "/GetVerifyCode", "{oldGuid:'" + oldGuid + "'}",
			 function(data) {
			 	var msg = eval('(' + data + ')');
			 	if (catalog == "login") {
			 		$("#hidVerifyCodeLogin").val(msg.d);
			 		$("#verImglogin").attr("src", VerifyCodeUrl + "?guid=" + msg.d)
			 	}
			 	else {
			 		$("#hidVerifyCodeReg").val(msg.d);
			 		$("#registerVerifyCodeImg").attr("src", VerifyCodeUrl + "?guid=" + msg.d);
			 	}
			 }, function() {

			 });
}


//----------------------------------Static Method-----------------------
$.ContentLoader = function(url, data, callback, error) {
	$.ajax({
		url: url,
		data: data,
		type: "POST",
		contentType: "application/json; charset=utf-8",
		success: callback,
		error: error
	});
};


